class Block{
public:
    float      x,y,z;float size;
    Vertex    *UFL,*UFR,*UBL,*UBR,*DFL,*DFR,*DBL,*DBR;
    Line      *UF,*UB,*UL,*UR,*FL,*FR,*BL,*BR,*DF,*DB,*DL,*DR,
                *U,*D,*F,*B,*L,*R;
    Triangle  *tUFL,*tUBR,*tDBL,*tDFR,*tFLU,*tFRD,*tBLD,*tBRU,*tLUF,*tLDB,*tRUB,*tRDF;
    Uint32    
        Ucolor=0xFFFF00,
        Dcolor=0xFFFFFF,
        Fcolor=0xFF8800,
        Bcolor=0xFF0000,
        Rcolor=0x00FF00,
        Lcolor=0x0000FF;


    Block():x(0.0),y(0.0),z(0.0),size(0.0){initData();}
    Block(float x_,float y_,float z_,float _size):x(x_),y(y_),z(z_),size(_size){initData();}
    void deleteVertex(){delete UFR;delete UFL;delete UBR;delete UBL;delete DFR;delete DFL;delete DBR;delete DBL;}
    void setsize(unsigned char _size){size=_size;}
    void initData(){
        UFR=new Vertex(x+size/2.0,y+size/2.0,z+size/2.0);
        UFL=new Vertex(x-size/2.0,y+size/2.0,z+size/2.0);
        UBR=new Vertex(x+size/2.0,y+size/2.0,z-size/2.0);
        UBL=new Vertex(x-size/2.0,y+size/2.0,z-size/2.0);
        DFR=new Vertex(x+size/2.0,y-size/2.0,z+size/2.0);
        DFL=new Vertex(x-size/2.0,y-size/2.0,z+size/2.0);
        DBR=new Vertex(x+size/2.0,y-size/2.0,z-size/2.0);
        DBL=new Vertex(x-size/2.0,y-size/2.0,z-size/2.0);
        UF=new Line(UFR,UFL,0xffffff);
        UB=new Line(UBR,UBL,0xffffff);
        UL=new Line(UFL,UBL,0xffffff);
        UR=new Line(UFR,UBR,0xffffff);
        FL=new Line(UFL,DFL,0xffffff);
        FR=new Line(UFR,DFR,0xffffff);
        BL=new Line(UBL,DBL,0xffffff);
        BR=new Line(UBR,DBR,0xffffff);
        DF=new Line(DFR,DFL,0xffffff);
        DB=new Line(DBR,DBL,0xffffff);
        DL=new Line(DFL,DBL,0xffffff);
        DR=new Line(DFR,DBR,0xffffff);
        U=new Line(UFR,UBL,Ucolor);
        D=new Line(DBR,DFL,Dcolor);
        F=new Line(DFL,UFR,Fcolor);
        B=new Line(UBL,DBR,Bcolor);
        L=new Line(UBL,DFL,Lcolor);
        R=new Line(UFR,DBR,Rcolor);
        tUFL=new Triangle(UFR,UFL,UBL,Ucolor);
        tUBR=new Triangle(UBL,UBR,UFR,Ucolor);
        tDBL=new Triangle(DBR,DBL,DFL,Dcolor);
        tDFR=new Triangle(DFL,DFR,DBR,Dcolor);
        tFLU=new Triangle(DFL,UFL,UFR,Fcolor);
        tFRD=new Triangle(UFR,DFR,DFL,Fcolor);
        tBLD=new Triangle(UBL,DBL,DBR,Bcolor);
        tBRU=new Triangle(DBR,UBR,UBL,Bcolor);
        tLUF=new Triangle(UBL,UFL,DFL,Lcolor);
        tLDB=new Triangle(DFL,DBL,UBL,Lcolor);
        tRUB=new Triangle(UFR,UBR,DBR,Rcolor);
        tRDF=new Triangle(DBR,DFR,UFR,Rcolor);
    }
    void operateBlock(float*mov,float*scale,char rotShaft,float theta){
        Vertex* Vs[8]={UFL,UFR,UBL,UBR,DFL,DFR,DBL,DBR};
        float x_=0,y_=0,z_=0;
        for (int i=0;i<8;i++){
            float x=Vs[i]->x,y=Vs[i]->y,z=Vs[i]->z;
            if (  mov!=0){x+=  mov[0];y+=  mov[1];z+=  mov[2];}
            if (scale!=0){x*=scale[0];y*=scale[1];z*=scale[2];}
            float a,b;
            switch (rotShaft){
                case 'x':
                case 'X':
                    a=cos(-theta*pi/180)* y-sin(-theta*pi/180)* z;
                    b=sin(-theta*pi/180)* y+cos(-theta*pi/180)* z;
                    y=a;z=b;break;
                case 'y':
                case 'Y':
                    a=cos(-theta*pi/180)* z-sin(-theta*pi/180)* x;
                    b=sin(-theta*pi/180)* z+cos(-theta*pi/180)* x;
                    z=a;x=b;break;
                case 'z':
                case 'Z':
                    a=cos(-theta*pi/180)* x-sin(-theta*pi/180)* y;
                    b=sin(-theta*pi/180)* x+cos(-theta*pi/180)* y;
                    x=a;y=b;break;
                default: break;
            }
            Vs[i]->x=x,Vs[i]->y=y,Vs[i]->z=z;
            x_+=x;y_+=y;z_+=z;
        }
        x=x_/8.0,y=y_/8.0,z=z_/8.0;
        tUFL->updateNormal();
        tUBR->updateNormal();
        tDBL->updateNormal();
        tDFR->updateNormal();
        tFLU->updateNormal();
        tFRD->updateNormal();
        tBLD->updateNormal();
        tBRU->updateNormal();
        tLUF->updateNormal();
        tLDB->updateNormal();
        tRUB->updateNormal();
        tRDF->updateNormal();
    }
    void draw(SDL_Renderer* renderer,SDL_Surface* ScSurface,Camara camara){
        UFR->WorldToCamara(camara);
        UFL->WorldToCamara(camara);
        UBR->WorldToCamara(camara);
        UBL->WorldToCamara(camara);
        DFR->WorldToCamara(camara);
        DFL->WorldToCamara(camara);
        DBR->WorldToCamara(camara);
        DBL->WorldToCamara(camara);
        // UF->draw(renderer,ScSurface,camara);
        // UB->draw(renderer,ScSurface,camara);
        // UL->draw(renderer,ScSurface,camara);
        // UR->draw(renderer,ScSurface,camara);
        // FL->draw(renderer,ScSurface,camara);
        // FR->draw(renderer,ScSurface,camara);
        // BL->draw(renderer,ScSurface,camara);
        // BR->draw(renderer,ScSurface,camara);
        // DF->draw(renderer,ScSurface,camara);
        // DB->draw(renderer,ScSurface,camara);
        // DL->draw(renderer,ScSurface,camara);
        // DR->draw(renderer,ScSurface,camara);
        tUFL->draw(renderer,ScSurface,camara);
        tUBR->draw(renderer,ScSurface,camara);
        tDBL->draw(renderer,ScSurface,camara);
        tDFR->draw(renderer,ScSurface,camara);
        tFLU->draw(renderer,ScSurface,camara);
        tFRD->draw(renderer,ScSurface,camara);
        tBLD->draw(renderer,ScSurface,camara);
        tBRU->draw(renderer,ScSurface,camara);
        tLUF->draw(renderer,ScSurface,camara);
        tLDB->draw(renderer,ScSurface,camara);
        tRUB->draw(renderer,ScSurface,camara);
        tRDF->draw(renderer,ScSurface,camara);
    }

};